/*
 * myradio_send.c
 *
 *  Created on: 2013.04.28.
 *      Author: Gábor
 */

#include "myradio_send.h"
//SEGMENT_VARIABLE( Pro2Cmd[16], U8, SEG_XDATA );
U8 Pro2Cmd[16];



si446x_reply_GET_INT_STATUS GET_INT_STATUS;

/*!
 * This function is used to show the actual state of the push-buttons on the LEDs.
 *
 * @return  TRUE - Started a packet TX / FALSE - Not packet to be sent.
 */
bool vSampleCode_SendFixPacket(void)
{

  fixRadioPacket[0]='B';
  fixRadioPacket[1]='U';
  fixRadioPacket[2]='T';
  fixRadioPacket[3]='T';
  fixRadioPacket[4]='O';
  fixRadioPacket[5]='N';
  fixRadioPacket[6]='1';

  vRadio_StartTx(0xFA, (U8 *) fixRadioPacket);

  /* Packet sending initialized */
  return true;
}

/*!
 *  Set Radio to TX mode, fixed packet length.
 *
 *  @param channel Freq. Channel, Packet to be sent
 *
 *  @note
 *
 */
void  vRadio_StartTx(U8 channel, U8 *pioFixRadioPacket)
{
  // Read ITs, clear pending ones
  si446x_get_int_status(0, 0, 0);

  /* Fill the TX fifo with datas */
//  si446x_write_tx_fifo(RadioConfiguration.Radio_PacketLength, pioFixRadioPacket);
  si446x_write_tx_fifo(0x07, pioFixRadioPacket);

  /* Start sending packet, channel 0, START immediately, Packet n bytes long, go READY when done */
 // si446x_start_tx(channel, 0x30,  RadioConfiguration.Radio_PacketLength);
  si446x_start_tx(channel, 0x30,0x07);

}

/*!
 * The function can be used to load data into TX FIFO.
 *
 * @param numBytes  Data length to be load.
 * @param pTxData   Pointer to the data (U8*).
 */
void si446x_write_tx_fifo(U8 numBytes, U8* pTxData)
{
	radio_comm_WriteData( SI446X_CMD_ID_WRITE_TX_FIFO, 0, numBytes, pTxData );
}

/*! Sends START_TX command to the radio.
 *
 * @param CHANNEL   Channel number.
 * @param CONDITION Start TX condition.
 * @param TX_LEN    Payload length (exclude the PH generated CRC).
 */
void si446x_start_tx(U8 CHANNEL, U8 CONDITION, U16 TX_LEN)
{
    Pro2Cmd[0] = SI446X_CMD_ID_START_TX;
    Pro2Cmd[1] = CHANNEL;
    Pro2Cmd[2] = CONDITION;
    Pro2Cmd[3] = (U8)(TX_LEN >> 8);
    Pro2Cmd[4] = (U8)(TX_LEN);

    radio_comm_SendCmd( SI446X_CMD_ARG_COUNT_START_TX, Pro2Cmd );
}

void si446x_get_int_status(U8 PH_CLR_PEND, U8 MODEM_CLR_PEND, U8 CHIP_CLR_PEND)
{
    Pro2Cmd[0] = 0x20;
    Pro2Cmd[1] = PH_CLR_PEND;
    Pro2Cmd[2] = MODEM_CLR_PEND;
    Pro2Cmd[3] = CHIP_CLR_PEND;

    radio_comm_SendCmdGetResp( 4,Pro2Cmd,8,Pro2Cmd );

    GET_INT_STATUS.INT_PEND       = Pro2Cmd[0];
    GET_INT_STATUS.INT_STATUS     = Pro2Cmd[1];
    GET_INT_STATUS.PH_PEND        = Pro2Cmd[2];
    GET_INT_STATUS.PH_STATUS      = Pro2Cmd[3];
    GET_INT_STATUS.MODEM_PEND     = Pro2Cmd[4];
    GET_INT_STATUS.MODEM_STATUS   = Pro2Cmd[5];
    GET_INT_STATUS.CHIP_PEND      = Pro2Cmd[6];
    GET_INT_STATUS.CHIP_STATUS    = Pro2Cmd[7];
}

void DemoApp_Pollhandler()
{
  static U8 lPktSending = 0u;

  // Check if the radio packet sent successfully
  if (true == gRadio_CheckTransmitted())
  {
    /* Clear Packet Sending flag */
    lPktSending = 0u;
  }

//  if ((lPer_SecCntr >= PACKET_SEND_INTERVAL) && (0u == lPktSending))
  //helyette csak packet sendinget vizsgálunk és timer szoftveres késleltetéssel-->köv sor for loop
  for(int dap = 0;dap<200000;dap++);
  if(lPktSending == 0)
  {
    if (true == vSampleCode_SendFixPacket())
    {
 //     lPer_SecCntr = 0u;

      /* Set Packet Sending flag */
      lPktSending = 1;
    }
  }
}
